﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using BookSystem.CommomHelper;
using BookSystem.Model;


namespace BookSystem.Admin
{
    public partial class AdminGetBook : Form
    {
        public AdminGetBook()
        {
            InitializeComponent();
        }


        SqlConnection conn = SQLHelper.getConnnection();
        SqlCommand cmd;

        string sql = "select * from T_books";


        private void dataBinding(String strSql)
        {

            SqlDataAdapter da;
            DataSet ds;


            //连接配置


            cmd = new SqlCommand(strSql, conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "myTable");

            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "myTable";
            da.Dispose();



        }

        private void AdminGetBook_Load(object sender, EventArgs e)
        {
            dataBinding(sql);
            dataGridView1.ReadOnly = true;

        }

        //dataGridView鼠标选中事件
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            textBox4.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();

        }


        //查询按钮
        private void get_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {

                MessageBox.Show("请输入关键字图书编号", "提示");
                textBox1.Focus();
                textBox1.SelectAll();
                return;

            }


            string sSQL = "select * from T_books where Bid = " + textBox1.Text;

            //刷新DataGridView控件
            dataBinding(sSQL);
        }

        //增加按钮
        private void add_Click(object sender, EventArgs e)
        {


            if (textBox1.Text != string.Empty
                && textBox2.Text != string.Empty
                && textBox3.Text != string.Empty
                && textBox4.Text != string.Empty)
            {
                String strSql = "insert into T_books values ('" + textBox1.Text + "','" +
                    textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";

                cmd = new SqlCommand(strSql, conn);

                try
                {
                    cmd.ExecuteNonQuery();

                }
                catch (SqlException sqlEx)
                {

                    MessageBox.Show("错误：可能是相同图书编号引起的！", sqlEx.ToString());

                }


                //刷新DataGridView控件
                dataBinding(sql);

            }
            else
            {

                return;

            }



        }

        //修改按钮
        private void update_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != string.Empty
                && textBox2.Text != string.Empty
                && textBox3.Text != string.Empty
                && textBox4.Text != string.Empty)
            {
                String strSql = "update T_books set Bname = '" + textBox2.Text + "',"
                    + "Bpress = '" + textBox3.Text + "',"
                    + "Author = '" + textBox4.Text + "'"
                    + "where Bid = " + "'" + textBox1.Text + "'";

                cmd = new SqlCommand(strSql, conn);


                cmd.ExecuteNonQuery();



                //刷新DataGridView控件
                dataBinding(sql);

            }
            else
            {

                return;

            }
        }

        //删除按钮
        private void delete_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != string.Empty)
            {
                String strSql = "delete from T_books where Bid = '" + textBox1.Text + "'";

                cmd = new SqlCommand(strSql, conn);

                cmd.ExecuteNonQuery();
            }

            //刷新DataGridView控件
            dataBinding(sql);
        }

        private void UserGetBook_FormClosed(object sender, FormClosedEventArgs e)
        {
            cmd.Dispose();
            conn.Close();
        }



    }
}
